﻿using System;
using System.Collections.Generic;
using System.Xml;

using TheOutfield.UmbExt.UniversalMediaPicker;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Interfaces;
using TheOutfield.UmbExt.UniversalMediaPicker.Providers;

namespace Our.Umbraco.UniversalMediaPicker.Providers.TwitPic
{
	/// <summary>
	/// 
	/// </summary>
	public class TwitPicProvider : AbstractProvider
	{
		/// <summary>
		/// 
		/// </summary>
		private TwitPicConfigControl configControl;

		/// <summary>
		/// Initializes a new instance of the <see cref="TwitPicProvider"/> class.
		/// </summary>
		public TwitPicProvider()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TwitPicProvider"/> class.
		/// </summary>
		/// <param name="config">The config.</param>
		public TwitPicProvider(string config)
			: base(config)
		{
			this.Service = new TwitPicService(config);
		}

		/// <summary>
		/// Gets or sets the service.
		/// </summary>
		/// <value>The service.</value>
		public TwitPicService Service { get; private set; }

		/// <summary>
		/// Gets the alias.
		/// </summary>
		/// <value>The alias.</value>
		public override string Alias
		{
			get
			{
				return "TwitPic";
			}
		}

		/// <summary>
		/// Gets the config control.
		/// </summary>
		/// <value>The config control.</value>
		public override IConfigControl ConfigControl
		{
			get
			{
				if (this.configControl == null)
				{
					this.configControl = new TwitPicConfigControl(this.Config);
				}

				return this.configControl;
			}
		}

		/// <summary>
		/// Gets the create control.
		/// </summary>
		/// <value>The create control.</value>
		public override ICreateControl CreateControl
		{
			get { throw new NotImplementedException(); }
		}

		/// <summary>
		/// Gets the media item by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public override MediaItem GetMediaItemById(string id)
		{
			if (!string.IsNullOrEmpty(id))
			{
				var image = this.Service.GetImage(id);
				if (image != null)
				{
					return image.ToMediaItem();
				}
			}

			return new MediaItem() { Id = "-1" }; // null;
		}

		/// <summary>
		/// Gets the media items.
		/// </summary>
		/// <param name="parentId">The parent id.</param>
		/// <returns></returns>
		public override IList<MediaItem> GetMediaItems(string parentId)
		{
			if (parentId.Equals("-1"))
			{
				return this.Service.GetImages();
			}

			return null;
		}
	}
}
